﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace CrossSelling.Core.ServiceCommand
{
    public class RetrieveSaleContestProgramServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveSaleContestProgramRequest, RetrieveSaleContestProgramResponse>
    {
        #region Properties
        public UserRecord Dao;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        public string Lang;
        #endregion
        public RetrieveSaleContestProgramServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveSaleContestProgramRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null || string.IsNullOrEmpty(request.CurrentUser.Dao))
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            Lang = request.Lang;
        }

        protected override bool OnExecute(RetrieveSaleContestProgramResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<ProductHandbookCategoryRecord> records = new List<ProductHandbookCategoryRecord>();
                List<SaleContestProgramInfo> listLoans = new List<SaleContestProgramInfo>();
                List<SaleContestProgram> listProductItem = Providers.ProductHandbooks.RetrieveAllSaleContestProgram(Dao);
                Dictionary<int, ProductCategory> listCategory = Providers.ProductHandbooks.RetrieveListCategory().ToDictionary(x => x.CategoryId);

                records = listProductItem.GroupBy(u => u.CategoryId)
                                                      .Select(grp => new ProductHandbookCategoryRecord
                                                      {
                                                          Category = Lang.Equals("en") ? listCategory[grp.Key].CategoryNameEn : listCategory[grp.Key].CategoryName,
                                                          Product = grp.Select(x => new ProductHandbookProductRecord
                                                          {
                                                              ProductId = x.SaleContestId,
                                                              ProductName = Lang.Equals("en") ? x.SaleContestName : x.SaleContestName,
                                                              HasData = x.HasData == 1,
                                                              HasInfo = x.HasInfo == 1
                                                          }).ToList()
                                                      })
                                                      .ToList();
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = 0,
                    PageSize = records.Count,
                    TotalRecords = records.Count
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
